﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;
using QUANLIBANHANG.ENTITY;

namespace QUANLIBANHANG.DAL
{
   public class NhanvienDAL
    {
       private static SqlConnection conn = ConnectionSQL.conn;
           //new SqlConnection(@"Data Source=(local);Initial Catalog=QUANLIBANHANG;Integrated Security=True");
       
       public static void insertNV(NhanvienEntity nv)
       {
           string sql= "INSERT INTO NHANVIEN (HOTEN, NGAYVL, SDT, AVATAR, NGAYSINH, EMAIL, DIACHI, PASS, CHUCVU) VALUES (@HOTEN,@NGAYVL,@SDT,@AVATAR,@NGAYSINH,@EMAIL,@DIACHI, @PASS, @CHUCVU)";
           try
           {
               conn.Open();
               SqlCommand command = new SqlCommand(sql, conn);
               command.Parameters.AddWithValue("@HOTEN", nv.Hoten);
               command.Parameters.AddWithValue("@NGAYVL", nv.Ngayvl);
               command.Parameters.AddWithValue("@SDT", nv.Sdt);
               command.Parameters.Add("@AVATAR", SqlDbType.Image, nv.Avatar.Length).Value = nv.Avatar;
               command.Parameters.AddWithValue("@NGAYSINH", nv.Ngaysinh);
               command.Parameters.AddWithValue("@EMAIL", nv.Email);
               command.Parameters.AddWithValue("@DIACHI", nv.Diachi);
               command.Parameters.AddWithValue("@PASS", nv.Pass);
               command.Parameters.AddWithValue("@CHUCVU", nv.Chucvu);
               command.ExecuteNonQuery();
               conn.Close();

           }
           catch (Exception ex)
           {

               throw ex;
           }
           finally { conn.Close(); }
       }
       public static void updateNV(NhanvienEntity nv)
       {
           string sql= "UPDATE NHANVIEN SET  HOTEN =@HOTEN, SDT =@SDT, NGAYVL =@NGAYVL, AVATAR =@AVATAR, NGAYSINH =@NGAYSINH, EMAIL =@EMAIL, DIACHI =@DIACHI, CHUCVU=@CHUCVU WHERE MANV='"+nv.Manv+"'";
           try
           {
               conn.Open();
               SqlCommand command = new SqlCommand(sql, conn);
               command.Parameters.AddWithValue("@HOTEN", nv.Hoten);
               command.Parameters.AddWithValue("@SDT", nv.Sdt);
               command.Parameters.AddWithValue("@NGAYVL", nv.Ngayvl);
               command.Parameters.Add("@AVATAR", SqlDbType.Image, nv.Avatar.Length).Value = nv.Avatar;
               command.Parameters.AddWithValue("@NGAYSINH", nv.Ngaysinh);
               command.Parameters.AddWithValue("@EMAIL", nv.Email);
               command.Parameters.AddWithValue("@DIACHI", nv.Diachi);
              
               command.Parameters.AddWithValue("@CHUCVU", nv.Chucvu);
               command.ExecuteNonQuery();
               conn.Close();
           }
           catch (Exception ex)
           {

               throw ex;
           }
           finally
           {
               conn.Close();
           }
       }
       public static void deleteNV(NhanvienEntity nv)
       {
           try
           {
               ConnectionSQL.commandSQL("DELETE FROM NHANVIEN WHERE MANV='" + nv.Manv + "'");
           }
           catch (Exception ex)
           {
               
               throw ex;
           }
       }
       public static DataTable getTableNV(string dk)
       {
           try
           {
               return ConnectionSQL.getTable("SELECT * FROM NHANVIEN " + dk);
               
           }
           catch (Exception ex)
           {
               
               throw ex;
           }
       }
       public static void updatePass(NhanvienEntity nv)
       {
           try
           {
               ConnectionSQL.commandSQL("UPDATE NHANVIEN SET PASS='" + nv.Pass + "' WHERE MANV='" + nv.Manv + "'");
           }
           catch (Exception ex)
           {
               
               throw ex;
           }
       }

    }
}
